﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;
using System.Linq;

namespace Samples.Radio.Helpers.Converters
{
    public class ListItemNullConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ien = value as IEnumerable;
            if (ien == null) return value;

            var ienum = ien.OfType<object>();

            if (parameter == null) return ienum.FirstOrDefault();
            try
            {
                var index = int.Parse(parameter as string);
                return ienum.Skip(index).FirstOrDefault();
            }
            catch
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
